//
//  LoginViewController.m
//  ChineseCollegeStudentsNetwork
//
//  Created by 刘喆 on 2018/5/9.
//  Copyright © 2018年 刘喆. All rights reserved.
//

#import "LoginViewController.h"
#import "RegisterViewController.h"
#import "CEAccessoryTextField.h"

#import "RegisterViewController.h"
#import "SJNetworkHelper.h"
#import "LoginRequest.h"
#import "MLAWebVC.h"

@interface LoginViewController ()<UITextFieldDelegate>

/** 标题 */
@property(nonatomic, strong) UILabel *titleLabel;
/** 用户名 */
@property(nonatomic, strong) CEAccessoryTextField *usernameTextField;
/** 密码 */
@property(nonatomic, strong) CEAccessoryTextField *passwordTextField;
/** 登录按钮 */
@property(nonatomic, strong) UIButton *loginButton;
@property (nonatomic, strong) UIButton *protocolBtn;


@end

@implementation LoginViewController

#pragma mark - Life Cycle

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"登录";
    [self setupConstraints];
    
    [self setupUI];
    
    _protocolBtn= [UIButton buttonWithType:UIButtonTypeCustom];
    [_protocolBtn setTitle:@"《大学生杂志社》用户隐私协议" forState:UIControlStateNormal];
    [_protocolBtn setTitleColor:kColorTint forState:UIControlStateNormal];
    _protocolBtn.titleLabel.font = [UIFont systemFontOfSize:12];
    _protocolBtn.titleLabel.textAlignment = NSTextAlignmentCenter;
    [_protocolBtn addTarget:self action:@selector(protocolAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_protocolBtn];
    [_protocolBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(200);
        make.height.mas_equalTo(40);
        make.centerX.equalTo(self.view);
        make.top.equalTo(_loginButton.mas_bottom).offset(10);
    }];
}

- (void)protocolAction {
    MLAWebVC *webVC = [[MLAWebVC alloc] init];
    webVC.urlStr = @"http://www.chinacampus.org/contents/66/827.html";
    webVC.titleStr = _protocolBtn.titleLabel.text;
    [self.navigationController pushViewController:webVC animated:YES];
}

- (void)setupUI {
    WEAK_SELF
    [self setupRightWithTitle:@"注册" actionBlock:^{
        STRONG_SELF
        RegisterViewController *registerVC = [[RegisterViewController alloc] init];
        [self.navigationController pushViewController:registerVC animated:YES];
    }];
}


- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    
    [self resignAllTextFieldResponder];
    
}

#pragma mark - Constraints

- (void)setupConstraints {
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(40);
        make.left.equalTo(self.view).offset(20);
        make.centerX.equalTo(self.view);
        make.height.mas_equalTo(@50);
    }];
    
    [self.usernameTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.titleLabel.mas_bottom).offset(50);
        make.centerX.equalTo(self.view);
        make.left.equalTo(self.view).offset(0);
        make.right.equalTo(self.view).offset(0);
        make.height.mas_equalTo(@50);
    }];
    
    [self.passwordTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.usernameTextField.mas_bottom).offset(20);
        make.centerX.equalTo(self.view);
        make.left.equalTo(self.usernameTextField);
        make.right.equalTo(self.usernameTextField);
        make.height.mas_equalTo(@50);
    }];
    
    [self.loginButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.passwordTextField.mas_bottom).offset(70);
        make.left.mas_equalTo(20);
        make.right.mas_equalTo(-20);
        make.height.mas_equalTo(@50);
    }];
}

#pragma mark - Lazy Loading

-  (UILabel *)titleLabel {
    
    if (!_titleLabel) {
        
        _titleLabel = [[UILabel alloc] init];
        _titleLabel.font = [UIFont systemFontOfSize:25];
        _titleLabel.textAlignment = NSTextAlignmentLeft;
        _titleLabel.textColor = [UIColor blackColor];
        _titleLabel.text = @"用户登录";
        
        [self.view addSubview:_titleLabel];
    }
    return _titleLabel;
}

- (CEAccessoryTextField *)usernameTextField {
    
    if (!_usernameTextField) {
        
        _usernameTextField = [[CEAccessoryTextField alloc] init];
        _usernameTextField.type = CEAccessoryTextFieldTypeNone;
        _usernameTextField.placeholder = @"用户名/邮箱/手机号";
        _usernameTextField.delegate = self;
        [self.view addSubview:_usernameTextField];
    }
    return _usernameTextField;
}

- (CEAccessoryTextField *)passwordTextField {
    
    if (!_passwordTextField) {
        
        _passwordTextField = [[CEAccessoryTextField alloc] init];
        _passwordTextField.type = CEAccessoryTextFieldTypeNone;
        _passwordTextField.placeholder = @"密码";
        _passwordTextField.secureTextEntry = YES;
        _passwordTextField.delegate = self;
        [self.view addSubview:_passwordTextField];
    }
    return _passwordTextField;
}

- (UIButton *)loginButton {
    
    if (!_loginButton) {
        
        _loginButton = [[UIButton alloc] init];
        _loginButton.backgroundColor = SMCTHEME;
        _loginButton.layer.cornerRadius = 5.0f;
        _loginButton.layer.masksToBounds = YES;
        
        [_loginButton setTitle:@"登录" forState:UIControlStateNormal];
        [_loginButton setTitle:@"登录" forState:UIControlStateHighlighted];
        
        @weakify(self);
        [[_loginButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
            @strongify(self);
            
            if([self.usernameTextField.text isEqualToString:@""] || !self.usernameTextField.text) {
                ShowMessage(@"请输入用户名");
            } else if([self.passwordTextField.text isEqualToString:@""] || !self.passwordTextField.text) {
                ShowMessage(@"请输入密码");
            } else {
                
                [self loginWR];
                
            }
        }];
        
        [self.view addSubview:_loginButton];
    }
    return _loginButton;
}

#pragma mark - UITextFieldDelegate

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    
    [textField resignFirstResponder];
    
    return YES;
}

#pragma mark - UIControl

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self resignAllTextFieldResponder];
}

#pragma mark - Private

- (void)resignAllTextFieldResponder {
    
    [self.usernameTextField resignFirstResponder];
    [self.passwordTextField resignFirstResponder];
}

#pragma mark - Network request

- (void)loginWR {
    
    NSDictionary *param = @{@"account": self.usernameTextField.text,
                            @"password": [self.passwordTextField.text md5String]};

    
    [MLAHttpManage postHttpType:MLAHttpRequestType_Post hostType:MLAHttpHostType_Api api:@"/api/v1/users/actions/login" params:param.mj_JSONString success:^(id  _Nonnull response, NSString * _Nonnull msg) {
        NSString *accessToken = response[@"accessToken"];
        [[NSUserDefaults standardUserDefaults] setObject:accessToken forKey:kAccessToken];
        [[NSUserDefaults standardUserDefaults] setObject:response[@"value"][@"id"] forKey:@"User_Id"];
        [self.navigationController popViewControllerAnimated:YES];
        

    } failure:^{
        ShowMessage(@"账号或密码错误");
    }];

    
}



//获取购物车
- (void) GetShoppingCartNetworkRequset {
    [[SJNetworkHelper sharedManager] getshoppingWithAccessToken:@"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJVc2VyTmFtZSI6ImRlbW8iLCJBZGREYXRlIjoiXC9EYXRlKDE1MjkzNjk5NTI4MjUpXC8ifQ.YDCYkjtqvgTQCXQgDJQ4ZpVTrpYnJObiZ0bL6owZ-Ws" success:^{
        NSLog(@"123");
    } failure:^(NSError *err) {
        NSLog(@"456");
    }];
    
}

//保存购物车
- (void) SaveShoppingCartNetworkRequset {
    
    [[SJNetworkHelper sharedManager] saveshoppingWithAccessToken:@"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJVc2VyTmFtZSI6ImRlbW8iLCJBZGREYXRlIjoiXC9EYXRlKDE1MjkzNjk5NTI4MjUpXC8ifQ.YDCYkjtqvgTQCXQgDJQ4ZpVTrpYnJObiZ0bL6owZ-Ws" success:^{
        NSLog(@"123");
    } failure:^(NSError *err) {
        NSLog(@"456");
    }];
}

//  添加收货地址
- (void) AddAddressNetworkRequset {
    
    [[SJNetworkHelper sharedManager] addAddressWithAccessToken:@"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJVc2VyTmFtZSI6ImRlbW8iLCJBZGREYXRlIjoiXC9EYXRlKDE1MjkzNjk5NTI4MjUpXC8ifQ.YDCYkjtqvgTQCXQgDJQ4ZpVTrpYnJObiZ0bL6owZ-Ws" success:^{
        NSLog(@"123");
    } failure:^(NSError *err) {
        NSLog(@"456");
    }];
    
}


//   新增订单
- (void)newOrderNetworkRequset {
    
    [[SJNetworkHelper sharedManager]neworderWithAccessToken:@"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJVc2VyTmFtZSI6ImRlbW8iLCJBZGREYXRlIjoiXC9EYXRlKDE1MjkzNjk5NTI4MjUpXC8ifQ.YDCYkjtqvgTQCXQgDJQ4ZpVTrpYnJObiZ0bL6owZ-Ws" success:^{
        NSLog(@"123");
    } failure:^(NSError *err) {
        NSLog(@"456");
    }];
    
}

//   获取订单
-(void)getOrderNetworkRequset {
    
    [[SJNetworkHelper sharedManager]getorderWithAccessToken:@"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJVc2VyTmFtZSI6ImRlbW8iLCJBZGREYXRlIjoiXC9EYXRlKDE1MjkzNjk5NTI4MjUpXC8ifQ.YDCYkjtqvgTQCXQgDJQ4ZpVTrpYnJObiZ0bL6owZ-Ws" success:^{
        NSLog(@"123");
    } failure:^(NSError *err) {
        NSLog(@"456");
    }];
}

//   获取全部订单

- (void)getAllOrderNetworkRequset {
    
    [[SJNetworkHelper sharedManager]getallorderWithAccessToken:@"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJVc2VyTmFtZSI6ImRlbW8iLCJBZGREYXRlIjoiXC9EYXRlKDE1MjkzNjk5NTI4MjUpXC8ifQ.YDCYkjtqvgTQCXQgDJQ4ZpVTrpYnJObiZ0bL6owZ-Ws" success:^{
        NSLog(@"123");
    } failure:^(NSError *err) {
        NSLog(@"456");
    }];
}

//   删除订单

- (void)removeOrderNetworkRequset {
    
    [[SJNetworkHelper sharedManager]removeorderWithAccessToken:@"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJVc2VyTmFtZSI6ImRlbW8iLCJBZGREYXRlIjoiXC9EYXRlKDE1MjkzNjk5NTI4MjUpXC8ifQ.YDCYkjtqvgTQCXQgDJQ4ZpVTrpYnJObiZ0bL6owZ-Ws" success:^{
        NSLog(@"123");
    } failure:^(NSError *err) {
        NSLog(@"456");
    }];
    
}




@end
